package com.broheim.websocket.core.listener;

import com.broheim.websocket.core.event.accept.Event;

import java.util.List;

public class ListenerChain {

    private int position = 0;
    private int size;
    private Listener[] listeners;

    public ListenerChain(List<Listener> listenerList) {
        this.listeners = new Listener[listenerList.size()];
        for (int i = 0; i < listenerList.size(); i++) {
            this.listeners[i] = listenerList.get(i);
        }
        this.size = this.listeners.length;
    }

    public void doChain(Event event) throws Exception {
        if (this.position < this.size) {
            Listener listener = this.listeners[this.position++];
            listener.onEvent(event, this);
        }
    }
}
